package com.ruoyi.eqmonitor.dispose.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.eqmonitor.dispose.dtos.*;
import com.ruoyi.eqmonitor.dispose.service.ExteriorSiteMonitorDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Api(tags = "微站站点监测数据")
@RestController
@RequestMapping("/siteMonitorData")
public class ExteriorSiteMonitorDataController {

    @Resource
    private ExteriorSiteMonitorDataService siteMonitorDataService;

    /**
     * 查询微站站点监测数据列表
     */
    @ApiOperation("查询微站站点监测数据列表")
    @GetMapping("/info")
    public AjaxResult getInfo(SiteMonitorDataDto siteMonitorDataDto){
        return AjaxResult.success(siteMonitorDataService.getInfo(siteMonitorDataDto));
    }

    /**
     * 根据站点编码查询最新的监测数据
     */
    @ApiOperation("根据站点编码查询最新的监测数据")
    @GetMapping("/lastData/{siteCode}")
    public AjaxResult lastData(@PathVariable("siteCode") String siteCode){
        return AjaxResult.success(siteMonitorDataService.lastData(siteCode));
    }

    /**
     * 根据站点名称查询最新的监测数据
     */
    @ApiOperation("根据站点名称查询最新的监测数据")
    @GetMapping("/lastListData")
    public AjaxResult lastListData(@RequestParam("siteName") String siteName){
        return AjaxResult.success(siteMonitorDataService.lastListData(siteName));
    }

    /**
     * 查询全部微站最近数据
     */
    @ApiOperation("查询全部微站最近数据")
    @GetMapping("/getAll")
    public AjaxResult getAll(){
        return AjaxResult.success(siteMonitorDataService.getAll());
    }

    /**
     * 微站水质数据(河道监测分析）
     */
    @ApiOperation("微站水质数据")
    @GetMapping("/getWaterQuality")
    public AjaxResult getWaterQuality(SiteMonitorDataDto dto){
        return AjaxResult.success(siteMonitorDataService.getWaterQuality(dto));
    }

    /**
     * 微站水质数据-前一个月
     */
    @ApiOperation("微站水质数据-前一个月")
    @GetMapping("/getWaterQualityLastMonth")
    public AjaxResult getWaterQualityLastMonth(SiteMonitorDataDto dto) throws ParseException {
        //处理时间参数
        String beginTimeString = dto.getBeginTime();
        String endTimeString = dto.getEndTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginTimeDate = sdf.parse(beginTimeString);
        Date endTimeDate = sdf.parse(endTimeString);
        //获取上个月时间
        Calendar c = Calendar.getInstance();
        c.setTime(beginTimeDate);
        c.add(Calendar.MONTH, -1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(endTimeDate);
        c2.add(Calendar.MONTH, -1);

        dto.setBeginTime(sdf.format(c.getTime()));
        dto.setEndTime(sdf.format(c2.getTime()));
        return AjaxResult.success(siteMonitorDataService.getWaterQuality(dto));
    }

    /**
     * 微站水质数据-后台
     */
    @ApiOperation("微站水质数据-后台")
    @GetMapping("/getWaterQualityBackstage")
    public AjaxResult getWaterQualityBackstage(String name){
        return AjaxResult.success(siteMonitorDataService.getWaterQualityBackstage(name));
    }

    /**
     * 微站水质数据-导出
     */
    @ApiOperation("微站水质数据-导出")
    @Log(title = "微站水质数据-导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getData/export")
    public void export(HttpServletResponse response, SiteMonitorDataDto dto) throws ParseException {
        List<SiteMonitorDataGetAllDto> list = siteMonitorDataService.getWaterQualityBackstageExport(dto);
        ExcelUtil<SiteMonitorDataGetAllDto> util = new ExcelUtil<SiteMonitorDataGetAllDto>(SiteMonitorDataGetAllDto.class);
        util.exportExcel(response, list, "微站水质数据");
    }

    /**
     * 微站水质数据-后台-导出
     */
    @ApiOperation("微站水质数据-后台-导出")
    @Log(title = "微站水质数据-后台-导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getWaterQualityBackstage/export")
    public void getWaterQualityBackstageExport(HttpServletResponse response, String name) throws ParseException {
        List<SiteMonitorDataGetAllVo> list = siteMonitorDataService.getWaterQualityBackstage(name);
        ExcelUtil<SiteMonitorDataGetAllVo> util = new ExcelUtil<SiteMonitorDataGetAllVo>(SiteMonitorDataGetAllVo.class);
        util.exportExcel(response, list, "微站水质数据");
    }

    /**
     * 微站水质数据-年月日报表
     */
    @ApiOperation("微站水质数据-年月日报表")
    @GetMapping("/getStatement")
    public AjaxResult getStatement(SiteMonitorDataDto dto) throws ParseException {
        return AjaxResult.success(siteMonitorDataService.getStatementExport(dto));
    }

    /**
     * 微站水质数据-年月日报表-导出
     */
    @ApiOperation("微站水质数据-年月日报表-导出")
    @Log(title = "微站水质数据-年月日报表-导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getStatement/export")
    public void getStatementExport(HttpServletResponse response,SiteMonitorDataDto dto) throws ParseException {
        List<StatementExportVo> list = siteMonitorDataService.getStatementExport(dto);
        ExcelUtil<StatementExportVo> util = new ExcelUtil<StatementExportVo>(StatementExportVo.class);
        util.exportExcel(response, list, "微站水质数据-年月日报表");
    }

    /**
     * 实时-当天-当月数据
     * @param state 0实时，1当天，2当前时间往前一个月
     * @return
     */
    @ApiOperation("实时-当天-当月数据")
    @GetMapping("/getHistory")
    public AjaxResult getHistory(String siteName,Integer state) {
        return AjaxResult.success(siteMonitorDataService.getHistory(siteName,state));
    }

    /**
     * 微站水质数据-年月日实时整合
     */
    @ApiOperation("微站水质数据-年月日实时整合")
    @RequestMapping("/getWaterQualityMerge")
    public AjaxResult getWaterQualityMerge(@RequestBody WaterQualityMergeDto dto) {
        return AjaxResult.success(siteMonitorDataService.getWaterQualityMerge(dto));
    }

    /**
     * 微站水质数据-年月日实时整合-导出
     */
    @ApiOperation("微站水质数据-年月日实时整合-导出")
    @Log(title = "微站水质数据-年月日实时整合-导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getWaterQualityMerge/export")
    public void getWaterQualityMergeExport(HttpServletResponse response,WaterQualityMergeDto dto) {
        List<WaterQualityMergeVO> list = siteMonitorDataService.getWaterQualityMerge(dto);
        ExcelUtil<WaterQualityMergeVO> util = new ExcelUtil<WaterQualityMergeVO>(WaterQualityMergeVO.class);
        util.exportExcel(response, list, "微站水质数据-年月日实时整合");
    }

    /**
     * 微站水质数据-年月日实时整合-改
     */
    @ApiOperation("微站水质数据-年月日实时整合-改")
    @RequestMapping("/getWaterQualityMergeG")
    public AjaxResult getWaterQualityMergeG(@RequestBody WaterQualityMergeDto dto) {
        return AjaxResult.success(siteMonitorDataService.getWaterQualityMergeG(dto));
    }

    /**
     * 微站水质数据-年月日实时整合-导出-改
     */
    @ApiOperation("微站水质数据-年月日实时整合-导出-改")
    @Log(title = "微站水质数据-年月日实时整合-导出-改", businessType = BusinessType.EXPORT)
    @PostMapping("/getWaterQualityMergeG/export")
    public void getWaterQualityMergeGExport(HttpServletResponse response,WaterQualityMergeDto dto) {
        List<WaterQualityMergeVO> list = siteMonitorDataService.getWaterQualityMergeG(dto);
        ExcelUtil<WaterQualityMergeVO> util = new ExcelUtil<WaterQualityMergeVO>(WaterQualityMergeVO.class);
        util.exportExcel(response, list, "微站水质数据-年月日实时整合");
    }

}
